#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<ranges>
using namespace std;

class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit)
    {
        ranges::sort(people);
        int n = people.size();
        int boat = 0;
        int left = 0, right = n - 1;
        while (left <= right)
        {
            if (left == right)
            {
                boat++;
                break;
            }
            if (people[left] + people[right] <= limit)
            {
                boat++;
                left++;
                right--;
            }
            else
            {
                boat++;
                right--;
            }
        }
        return boat;
    }
};